Answer
In [13]:
def calculate_weight(feature):
weight = (1/(max(feature) - min(feature))) ** 2
return weight
price = calculate_weight(np.array([500000, 350000, 600000, 400000], dtype=float))
room = calculate_weight(np.array([3, 2, 4, 2], dtype=float))
lot = calculate_weight(np.array([1840, 1600, 2000, 1900], dtype=float))
print price
print room
print lot
Answer
In [16]:
import numpy as np
s1 = np.array([2, 1, 1, 1, 1, 1, 1, 1, 0, 0], dtype=float)
s2 = np.array([0, 2, 1, 1, 0, 0, 0, 1, 2, 1], dtype=float)
print s1
print s2
euclidean_distance = np.sqrt(np.sum((s1 - s2)**2))
euclidean_distance
Out[16]:
In [17]:
import numpy as np
s1 = np.array([2, 1, 1, 1, 1, 1, 1, 1, 0, 0], dtype=float)
s2 = np.array([0, 2, 1, 1, 0, 0, 0, 1, 2, 1], dtype=float)
print s1
print s2
cosine_similarity = np.dot(s1, s2)/(np.sqrt(np.sum(s1**2)) * np.sqrt(np.sum(s2**2)))
cosine_distance = 1 - cosine_similarity
cosine_distance
Out[17]:
Answer